package main

import (
	"fmt"
	"time"
)

/*
非缓冲通道make(chan T)
缓冲通道make(chan T capacity)
定向通道make(chan <- int)只能写 make(<-chan int) 只能读
*/
var a chan bool

func printNum() {
	for i := range 1000 {
		fmt.Printf("数字是:%d\n", i)
	}
	a <- true
}
func sendData(ch1 chan int) {
	for i := range 10 {
		ch1 <- i

	}
	close(ch1)

}
func main() {
	a = make(chan bool)
	start := time.Now().Nanosecond()
	//printNum()
	go printNum()
	for i := 0; i < 1000; i++ {
		fmt.Println("字母A")
	}
	data := <-a
	end := time.Now().Nanosecond()
	fmt.Println("gap", end-start, data)
	fmt.Println("main over")
	ch1 := make(chan int)
	go sendData(ch1)

	//另一种写法
	for v := range ch1 {
		fmt.Println(v)

	}
	for i := 0; i < 1000; i++ {
		v, ok := <-ch1
		if !ok {
			fmt.Println("读取完数据", ok)
			break
		}
		fmt.Println(v)
	}

	ch3 := make(chan int)
	ch2 := make(chan int)
	go func() {
		ch3 <- 100
	}()
	go func() {
		ch2 <- 500
	}()
	select {
	case num1 := <-ch3:
		fmt.Println("选择了num1:", num1)
	case num2 := <-ch2:
		fmt.Println("选择了num2", num2)

	default:
		fmt.Println("hello")

	}
}
